package com.mcxx.modules.tool.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.mcxx.common.ExternalFileUtil;
import com.mcxx.modules.external.guicai.entity.SHJZT206Y19Entity;
import com.mcxx.modules.external.guicai.entity.SHJZT207Y19Entity;
import com.mcxx.modules.external.guicai.service.write.SHJZT204Y19WriteService;
import com.mcxx.modules.external.guicai.service.write.SHJZT205Y19WriteService;
import com.mcxx.modules.external.guicai.service.write.SHJZT206Y19WriteService;
import com.mcxx.modules.external.guicai.service.write.SHJZT207Y19WriteService;
import com.mcxx.modules.miniSalvation.family.dto.FamilyMemberDTO;
import com.mcxx.modules.miniSalvation.family.entity.BankEntity;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMainHistoryEntity;
import com.mcxx.modules.miniSalvation.family.service.read.BankReadService;
import com.mcxx.modules.miniSalvation.family.service.read.FamilyMainHistoryReadService;
import com.mcxx.modules.miniSalvation.family.service.read.FamilyMemberHistoryReadService;
import com.mcxx.modules.miniSalvation.fundpay.dto.FundPayFamilyDTO;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayEntity;
import com.mcxx.modules.miniSalvation.fundpay.service.read.FundPayFamilyReadService;
import com.mcxx.modules.miniSalvation.order.entity.OrderEntity;
import com.mcxx.modules.miniSalvation.order.entity.StandardEntity;
import com.mcxx.modules.miniSalvation.order.service.read.OrderReadService;
import com.mcxx.modules.miniSalvation.order.service.read.StandardReadService;
import com.mcxx.modules.miniSalvation.salary.entity.MemberSalaryEntity;
import com.mcxx.modules.miniSalvation.salary.service.read.FamilyMemberSalaryReadService;
import com.mcxx.modules.statistics.entity.StatisticsExportEntity;
import com.mcxx.modules.system.param.StandardParam;
import com.mcxx.modules.system.service.read.StatisticsExportReadService;
import com.mcxx.modules.system.service.write.StatisticsExportWriteService;
import com.mcxx.util.Constant;
import com.mcxx.util.DateUtil;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


@Service
@TaTransactional
public class GuicaiExtremeService {

    private final static org.slf4j.Logger logger = LoggerFactory.getLogger(GuicaiExtremeService.class);

    @Autowired
    private FundPayFamilyReadService fundPayFamilyReadService;
    @Autowired
    private OrderReadService orderReadService;
    @Autowired
    private FamilyMainHistoryReadService familyMainHistoryReadService;
    @Autowired
    private FamilyMemberSalaryReadService familyMemberSalaryReadService;
    @Autowired
    private FamilyMemberHistoryReadService familyMemberHistoryReadService;
    @Autowired
    private StandardReadService standardReadService;
    @Autowired
    private BankReadService bankReadService;
    @Autowired
    private SHJZT206Y19WriteService shJZT206Y19WriteService;
    @Autowired
    private SHJZT207Y19WriteService shJZT207Y19WriteService;
    @Autowired
    private StatisticsExportReadService statisticsExportReadService;
    @Autowired
    private StatisticsExportWriteService statisticsExportWriteService;





    private static String donePath = "E:/gc_ex_done.txt";
    private static String errPath = "E:/gc_error.txt";

    private final RejectedExecutionHandler THREAD_HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();
    private final ThreadPoolExecutor SMS_GATE_SERVE_DELIVER = new ThreadPoolExecutor(20,
            20, 3, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(50),
            THREAD_HANDLER);

    public void findFamilyByPay(String id, String strMonths, String areaCode, String guicaiType,
                                Map<String, List<FundPayEntity>> map,List<?> standCitySalvArr,List<?> allowanceCitySalvArr,List<?> sustomCitySalvArr){

        StandardParam p = new StandardParam();
        p.setStatus("1");
        p.setAreaCode(areaCode+"000000");
        Double standardMoney = 0.0;
        switch (guicaiType){
            case "206":
                List<FundPayEntity> arras310= map.get("310");
                List<FundPayEntity> arras330= map.get("330");
                List<FundPayFamilyDTO> reissueFamiltList = new ArrayList<>();

                List<FundPayFamilyDTO> excuteList310 = new ArrayList<>();
                for (Object aa : arras310){
                    FundPayEntity fp = (FundPayEntity) aa;
                    List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                    excuteList310.addAll(dtoByFundPayId);
                }
                List<FundPayFamilyDTO> excuteList330 = new ArrayList<>();
                if(CollectionUtils.isNotEmpty(arras330)){
                    for (Object aa : arras330){
                        FundPayEntity fp = (FundPayEntity) aa;
                        List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                        excuteList330.addAll(dtoByFundPayId);
                    }
                }

                if(CollectionUtils.isNotEmpty(standCitySalvArr)){
                    for (Object aa : standCitySalvArr){
                        FundPayEntity fp = (FundPayEntity) aa;
                        List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                        reissueFamiltList.addAll(dtoByFundPayId);
                    }
                }
                if(CollectionUtils.isNotEmpty(allowanceCitySalvArr)){
                    for (Object aa : allowanceCitySalvArr){
                        FundPayEntity fp = (FundPayEntity) aa;
                        List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                        reissueFamiltList.addAll(dtoByFundPayId);
                    }
                }

                if(CollectionUtils.isNotEmpty(sustomCitySalvArr)){
                    for (Object aa : sustomCitySalvArr){
                        FundPayEntity fp = (FundPayEntity) aa;
                        List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                        reissueFamiltList.addAll(dtoByFundPayId);
                    }
                }

                if(CollectionUtils.isNotEmpty(sustomCitySalvArr)){
                    for (Object aa : sustomCitySalvArr){
                        FundPayEntity fp = (FundPayEntity) aa;
                        List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                        reissueFamiltList.addAll(dtoByFundPayId);
                    }
                }

                if(CollectionUtils.isNotEmpty(excuteList310)){
                    p.setStandType("03");
                    List<StandardEntity> byParam = standardReadService.getByParam(p);
                    if(CollectionUtils.isNotEmpty(byParam)){
                        standardMoney = byParam.get(0).getStandardMoney();
                    }
                    Map<String, Double> uuidNameMap = new HashMap<>();
                    if(CollectionUtils.isNotEmpty(excuteList330)){
                        uuidNameMap = excuteList330.stream().collect(Collectors.toMap(FundPayFamilyDTO::getOrderId, FundPayFamilyDTO::getFundNumber, (item1, item2) -> item1));
                    }
                    Map<String, Double> reissueNameMap = new HashMap<>();
                    if(CollectionUtils.isNotEmpty(reissueFamiltList)){
                        reissueNameMap = reissueFamiltList.stream().collect(Collectors.toMap(FundPayFamilyDTO::getOrderId, FundPayFamilyDTO::getFundNumber, (item1, item2) -> item1));
                    }
                    SMS_GATE_SERVE_DELIVER.execute(new executeCityPoorData(reissueNameMap,excuteList310,uuidNameMap,standardMoney,areaCode,strMonths,id));
                }
                break;
            case "207":
                arras310= map.get("310");
                arras330= map.get("330");
                reissueFamiltList = new ArrayList<>();

                excuteList310 = new ArrayList<>();
                for (Object aa : arras310){
                    FundPayEntity fp = (FundPayEntity) aa;
                    List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                    excuteList310.addAll(dtoByFundPayId);
                }
                excuteList330 = new ArrayList<>();
                if(CollectionUtils.isNotEmpty(arras330)) {
                    for (Object aa : arras330) {
                        FundPayEntity fp = (FundPayEntity) aa;
                        List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                        excuteList330.addAll(dtoByFundPayId);
                    }
                }
                if(CollectionUtils.isNotEmpty(standCitySalvArr)){
                    for (Object aa : standCitySalvArr){
                        FundPayEntity fp = (FundPayEntity) aa;
                        List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                        reissueFamiltList.addAll(dtoByFundPayId);
                    }
                }
                if(CollectionUtils.isNotEmpty(allowanceCitySalvArr)){
                    for (Object aa : allowanceCitySalvArr){
                        FundPayEntity fp = (FundPayEntity) aa;
                        List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                        reissueFamiltList.addAll(dtoByFundPayId);
                    }
                }

                if(CollectionUtils.isNotEmpty(sustomCitySalvArr)){
                    for (Object aa : sustomCitySalvArr){
                        FundPayEntity fp = (FundPayEntity) aa;
                        List<FundPayFamilyDTO> dtoByFundPayId = fundPayFamilyReadService.getDTOByFundPayId(fp.getId());
                        reissueFamiltList.addAll(dtoByFundPayId);
                    }
                }
                if(CollectionUtils.isNotEmpty(excuteList310)){
                    p.setStandType("04");
                    List<StandardEntity> byParam = standardReadService.getByParam(p);
                    if(CollectionUtils.isNotEmpty(byParam)){
                        standardMoney = byParam.get(0).getStandardMoney();
                    }
                    Map<String, Double> uuidNameMap = new HashMap<>();
                    if(CollectionUtils.isNotEmpty(excuteList330)){
                        uuidNameMap = excuteList330.stream()
                                .collect(Collectors.toMap(FundPayFamilyDTO::getOrderId, FundPayFamilyDTO::getFundNumber));
                    }
                    Map<String, Double> reissueNameMap = new HashMap<>();
                    if(CollectionUtils.isNotEmpty(reissueFamiltList)){
                        reissueNameMap = reissueFamiltList.stream().collect(Collectors.toMap(FundPayFamilyDTO::getOrderId, FundPayFamilyDTO::getFundNumber, (item1, item2) -> item1));
                    }

                    SMS_GATE_SERVE_DELIVER.execute(new executeVillPoorData(reissueNameMap,excuteList310,uuidNameMap,standardMoney,areaCode,strMonths,id));
                }

                break;
        }
    }

    class executeCityPoorData implements Runnable{

        private List<FundPayFamilyDTO> list310;

        private Map<String, Double> nursingMap;

        private Double standardMoney;

        private String areaCode;

        private String strMonths;

        private String id;

        private Map<String, Double> reissueMap;

        public executeCityPoorData(Map<String, Double> reissueMap,List<FundPayFamilyDTO> list310,Map<String, Double> nursingMap,Double standardMoney,String areaCode,String strMonths,String id){
            this.list310 = list310;
            this.nursingMap = nursingMap;
            this.standardMoney = standardMoney;
            this.areaCode = areaCode;
            this.strMonths = strMonths;
            this.id = id;
            this.reissueMap = reissueMap;
        }

        @Override
        public void run() {
            try {
                if(CollectionUtils.isNotEmpty(list310)){
                    for (FundPayFamilyDTO pf : list310){
                        if(ExternalFileUtil.traverseFolder(donePath, pf.getOrderId())){
                            logger.info("oId 已处理");
                        }else{

                            if(StringUtils.isEmpty(id)){
                                StatisticsExportEntity en = new StatisticsExportEntity();
                                en.setObjectType("131");
                                en.setYears(strMonths);
                                en.setAreaCode(areaCode);
                                StatisticsExportEntity byYearATypeACode = statisticsExportReadService.getByYearATypeACode(en);
                                if(null == byYearATypeACode){
                                    id = Constant.getUUID();
                                    en.setId(id);
                                    en.setOrgId(areaCode+"000000");
                                    en.setExportTime(new Date());
                                    en.setStatus("0");
                                    statisticsExportWriteService.insertIgnoreNull(en);
                                }else{
                                    if(!"0".equals(byYearATypeACode.getStatus())){
                                        break;
                                    }
                                    id = byYearATypeACode.getId();
                                }
                            }
                            OrderEntity order = orderReadService.getById(pf.getOrderId());
                            if(null == order){
                                ExternalFileUtil.writeLog(errPath,"未查询到业务记录,orderId:"+pf.getOrderId());
                                continue;
                            }

                            FamilyMainHistoryEntity family = familyMainHistoryReadService.getByOrderId(pf.getOrderId());
                            if(null == family){
                                ExternalFileUtil.writeLog(errPath,"未查询到家庭历史记录,orderId:"+pf.getOrderId());
                                continue;
                            }

                            List<FamilyMemberDTO> memberList = familyMemberHistoryReadService.listByOrderId(pf.getOrderId());
                            if(CollectionUtils.isEmpty(memberList)){
                                ExternalFileUtil.writeLog(errPath,"未查询到成员历史记录,orderId:"+pf.getOrderId());
                                continue;
                            }

                            List<MemberSalaryEntity> memberSalary = familyMemberSalaryReadService.queryMemberItemSalaryByOrderId(pf.getOrderId());
                            if(CollectionUtils.isEmpty(memberSalary)){
                                ExternalFileUtil.writeLog(errPath,"未查询到成员待遇记录,orderId:"+pf.getOrderId());
                                continue;
                            }
                            FamilyMemberDTO famOwner = new FamilyMemberDTO();
                            for(FamilyMemberDTO mb : memberList){
                                if(mb.getFamilyRelation().equals("01")){
                                    famOwner=mb;
                                }
                            }

                            SHJZT206Y19Entity main = new SHJZT206Y19Entity();
                            main.setRECID(Constant.getUUID());
                            main.setORGUNIT(order.getAreaCode());

                            main.setENTERINGTIME(DateUtil.now());
                            main.setT206_DJSJ_F1_Y19(DateUtil.formatDate(order.getCommitDate()));
                            main.setSJBHSJ(DateUtil.date2Str(order.getCreateTime(),DateUtil.sdf));
                            main.setSJBHZT(order.getType().equals("01") ? "新申请" : (order.getType().equals("02") ? "复查" : "其他"));

                            main.setOrder_id(order.getOrderId());
                            main.setFamilyId(order.getFamilyId());
                            main.setT206_ZJHM_F2_Y19(family.getSalvationNo());
                            main.setT206_SFZH_F3_Y19(family.getIdcard());
                            main.setT206_HZXM_F4_Y19(family.getName());
                            main.setT206_XB_F5_Y19(famOwner.getSex());
                            main.setT206_MZ_F6_Y19(famOwner.getNation());

                            if(order.getRemark().length() > 50){
                                main.setT206_BZ_F27_Y19(order.getRemark().substring(0,50));
                            }else{
                                main.setT206_BZ_F27_Y19(order.getRemark());
                            }
                            if(family.getAddress().length() > 50){
                                main.setT206_ZZ_F7_Y19(family.getAddress().substring(0,50)); // = Server_1.GCServer.Left(this.familyHis.address, 50);
                            }else{
                                main.setT206_ZZ_F7_Y19(family.getAddress());
                            }

                            main.setT206_DH_F9_Y19(family.getPhone());
                            main.setT206_LB_F10_Y19(!family.equals(01) ? "4" : "3");

                            Integer[] cals = getNumbers206(memberList);
                            main.setT206_XSRS_F11_Y19(cals[0]);
                            main.setT206_NX_F15_Y19(cals[1]);
                            main.setT206_LNR_F12_Y19(cals[4]);
                            main.setT206_CNR_F13_Y19(cals[5]);
                            main.setT206_WCNR_F14_Y19(cals[6]);
                            main.setT206_CJR_F16_Y19(cals[2]);
                            main.setT206_QZL_F70_Y19(cals[7]);
                            main.setT206_BHL_F71_Y19(cals[8]);
                            main.setT206_QHL_F72_Y19(cals[9]);
                            Double[] monthMoney = getMonthMoney(Constant.BusinessCode.TEKUN,memberSalary);
                            Double careMoney = nursingMap.get(pf.getOrderId());
                            if(null == careMoney){
                                careMoney = 0.0;
                            }
                            Double reissueMoney = reissueMap.get(pf.getOrderId());
                            if(null == reissueMoney){
                                reissueMoney = 0.0;
                            }

                            main.setT206_HJ_F17_Y19(monthMoney[0] + careMoney + reissueMoney);
                            main.setT206_YBZ_F18_Y19(monthMoney[0]);
                            main.setT206_JTRP_F21_Y19(0.0);
                            main.setT206_JEE_F19_Y19(careMoney);
                            main.setT206_ZDBZ_F22_Y19(monthMoney[0] / main.getT206_XSRS_F11_Y19());
                            main.setT206_JES_F20_Y19(reissueMoney);


                            BankEntity bankByFamilyId = bankReadService.getBankByFamilyId(famOwner.getFamilyId(), famOwner.getMemberId(), order.getBusinessType());

                            if(null != bankByFamilyId){
                                main.setT206_KHYX_F23_Y19(bankByFamilyId.getMasterBankCode());
                                main.setT206_ZH_F24_Y19(bankByFamilyId.getMasterAccount());
                            }
                            main.setT206_HZSF_F60_Y19(family.getMasterInSave());

                            int no = 0;
                            for(FamilyMemberDTO m : memberList){
                                if(m.getMemberId() != famOwner.getMemberId() &&
                                        m.getIsSaveObject().equals("1") &&
                                        m.getState().equals("01")
                                ){
                                    setFamMember206(main, m, no);
                                    no++;
                                }
                            }
                            main.setStatisticsId(id);

                            shJZT206Y19WriteService.insertIgnoreNull(main);

                            ExternalFileUtil.writeLog(donePath,pf.getOrderId());
                        }
                    }
                    statisticsExportWriteService.updateStatusById(id,"1");
                }
            } catch (IOException e) {
                e.printStackTrace();
                ExternalFileUtil.writeLog(errPath,e.getMessage());
            }
        }
    }

    class executeVillPoorData implements Runnable{

        private List<FundPayFamilyDTO> list;

        private Map<String, Double> nursingMap;

        private Double standardMoney;

        private String areaCode;

        private String strMonths;

        private String id;

        private Map<String, Double> reissueMap;

        public executeVillPoorData(Map<String, Double> reissueMap,List<FundPayFamilyDTO> list,Map<String, Double> nursingMap,Double standardMoney,String areaCode,String strMonths,String id){
            this.list = list;
            this.nursingMap = nursingMap;
            this.standardMoney = standardMoney;
            this.areaCode = areaCode;
            this.strMonths = strMonths;
            this.id = id;
            this.reissueMap = reissueMap;
        }

        @Override
        public void run() {
            try {
                if(CollectionUtils.isNotEmpty(list)){
                    for (FundPayFamilyDTO pf : list){
                        if(ExternalFileUtil.traverseFolder(donePath, pf.getOrderId())){
                            logger.info("oId 已处理");
                        }else{

                            if(StringUtils.isEmpty(id)){
                                StatisticsExportEntity en = new StatisticsExportEntity();
                                en.setObjectType("132");
                                en.setYears(strMonths);
                                en.setAreaCode(areaCode);
                                StatisticsExportEntity byYearATypeACode = statisticsExportReadService.getByYearATypeACode(en);
                                if(null == byYearATypeACode){
                                    id = Constant.getUUID();
                                    en.setId(id);
                                    en.setOrgId(areaCode+"000000");
                                    en.setExportTime(new Date());
                                    en.setStatus("0");
                                    statisticsExportWriteService.insertIgnoreNull(en);
                                }else{
                                    if(!"0".equals(byYearATypeACode.getStatus())){
                                        break;
                                    }
                                    id = byYearATypeACode.getId();
                                }
                            }
                            OrderEntity order = orderReadService.getById(pf.getOrderId());
                            if(null == order){
                                ExternalFileUtil.writeLog(errPath,"未查询到业务记录,orderId:"+pf.getOrderId());
                                continue;
                            }

                            FamilyMainHistoryEntity family = familyMainHistoryReadService.getByOrderId(pf.getOrderId());
                            if(null == family){
                                ExternalFileUtil.writeLog(errPath,"未查询到家庭历史记录,orderId:"+pf.getOrderId());
                                continue;
                            }

                            List<FamilyMemberDTO> memberList = familyMemberHistoryReadService.listByOrderId(pf.getOrderId());
                            if(CollectionUtils.isEmpty(memberList)){
                                ExternalFileUtil.writeLog(errPath,"未查询到成员历史记录,orderId:"+pf.getOrderId());
                                continue;
                            }

                            List<MemberSalaryEntity> memberSalary = familyMemberSalaryReadService.queryMemberItemSalaryByOrderId(pf.getOrderId());
                            if(CollectionUtils.isEmpty(memberSalary)){
                                ExternalFileUtil.writeLog(errPath,"未查询到成员待遇记录,orderId:"+pf.getOrderId());
                                continue;
                            }
                            FamilyMemberDTO famOwner = new FamilyMemberDTO();
                            for(FamilyMemberDTO mb : memberList){
                                if(mb.getFamilyRelation().equals("01")){
                                    famOwner=mb;
                                }
                            }

                            SHJZT207Y19Entity main = new SHJZT207Y19Entity();
                            main.setRECID(Constant.getUUID());
                            main.setORGUNIT(order.getAreaCode());

                            main.setENTERINGTIME(DateUtil.now());
                            main.setT207_DJSJ_F1_Y19(DateUtil.formatDate(order.getCommitDate()));
                            main.setSJBHSJ(DateUtil.date2Str(order.getCreateTime(),DateUtil.sdf));
                            main.setSJBHZT(order.getType().equals("01") ? "新申请" : (order.getType().equals("02") ? "复查" : "其他"));

                            main.setOrder_id(order.getOrderId());
                            main.setFamilyId(order.getFamilyId());
                            main.setT207_ZJHM_F2_Y19(family.getSalvationNo());
                            main.setT207_SFZH_F3_Y19(family.getIdcard());
                            main.setT207_HZXM_F4_Y19(family.getName());
                            main.setT207_XB_F5_Y19(famOwner.getSex());
                            main.setT207_MZ_F6_Y19(famOwner.getNation());

                            if(order.getRemark().length() > 50){
                                main.setT207_BZ_F27_Y19(order.getRemark().substring(0,50));
                            }else{
                                main.setT207_BZ_F27_Y19(order.getRemark());
                            }
                            if(family.getAddress().length() > 50){
                                main.setT207_ZZ_F7_Y19(family.getAddress().substring(0,50)); // = Server_1.GCServer.Left(this.familyHis.address, 50);
                            }else{
                                main.setT207_ZZ_F7_Y19(family.getAddress());
                            }

                            main.setT207_DH_F9_Y19(family.getPhone());
                            main.setT207_LB_F10_Y19(!family.equals(01) ? "4" : "3");

                            Integer[] cals = getNumbers207(memberList);
                            main.setT207_XSRS_F11_Y19(cals[0]);
                            main.setT207_NX_F12_Y19(cals[1]);
                            main.setT207_LNR_F13_Y19(cals[4]);
                            main.setT207_CNR_F28_Y19(cals[5]);
                            main.setT207_WCNR_F14_Y19(cals[6]);
                            main.setT207_CJR_F15_Y19(cals[2]);
                            main.setT207_QZL_F70_Y19(cals[7]);
                            main.setT207_BHL_F71_Y19(cals[8]);
                            main.setT207_QHL_F72_Y19(cals[9]);

                            Double[] monthMoney = getMonthMoney(Constant.BusinessCode.TEKUN,memberSalary);

                            Double careMoney = nursingMap.get(pf.getOrderId());
                            if(null == careMoney){
                                careMoney = 0.0;
                            }
                            Double reissueMoney = reissueMap.get(pf.getOrderId());
                            if(null == reissueMoney){
                                reissueMoney = 0.0;
                            }
                            main.setT207_HJ_F16_Y19(monthMoney[0] + careMoney + reissueMoney);
                            main.setT207_YBZ_F17_Y19(monthMoney[0]);
                            main.setT207_JTRP_F20_Y19(0.0);
                            main.setT207_ZDBZ_F21_Y19(monthMoney[0] / main.getT207_XSRS_F11_Y19());
                            main.setT207_JEE_F18_Y19(careMoney);
                            main.setT207_JES_F19_Y19(reissueMoney);



                            BankEntity bankByFamilyId = bankReadService.getBankByFamilyId(famOwner.getFamilyId(), famOwner.getMemberId(), order.getBusinessType());

                            if(null != bankByFamilyId){
                                main.setT207_KHYX_F23_Y19(bankByFamilyId.getMasterBankCode());
                                main.setT207_ZH_F24_Y19(bankByFamilyId.getMasterAccount());
                            }

                            main.setT207_HZSF_F60_Y19(family.getMasterInSave());

                            int no = 0;
                            for(FamilyMemberDTO m : memberList){
                                if(m.getMemberId() != famOwner.getMemberId() &&
                                        m.getIsSaveObject().equals("1") &&
                                        m.getState().equals("01")
                                ){
                                    setFamMember207(main, m, no);
                                    no++;
                                }
                            }
                            main.setStatisticsId(id);

                            shJZT207Y19WriteService.insertIgnoreNull(main);

                            ExternalFileUtil.writeLog(donePath,pf.getOrderId());
                        }
                    }
                    statisticsExportWriteService.updateStatusById(id,"1");
                }
            } catch (IOException e) {
                e.printStackTrace();
                ExternalFileUtil.writeLog(errPath,e.getMessage());
            }
        }
    }

    private Integer[] getNumbers207(List<FamilyMemberDTO> list){

        Integer[] rnt = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        for (int i = 0; i< list.size(); i++) {
            FamilyMemberDTO dto = list.get(i);

            if (dto.getState().equals("01") && dto.getIsSaveObject().equals("1"))
            {
                rnt[0]++;
                if (null != dto.getBirthday()) {
                    long bir = dto.getBirthday().getTime();
                    long now = System.currentTimeMillis();
                    long chang = 31536000000L;
                    long age = (now - bir) / chang;
                    if (age >= 60)
                        rnt[4]++;
                    else if (age >= 18)
                        rnt[5]++;
                    else
                        rnt[6]++;
                }
                else {
                    rnt[5]++;
                }
                if (!StringUtils.isEmpty(dto.getSex()) && dto.getSex().equals("2"))
                    rnt[1]++;
                if (!StringUtils.isEmpty(dto.getDisabilityType())) {
                    rnt[2]++;
                }
                if (!StringUtils.isEmpty(dto.getSelfcareLevel())) {
                    if (dto.getSelfcareLevel().equals("01"))
                        rnt[7]++;
                    else if (dto.getSelfcareLevel().equals("02"))
                        rnt[8]++;
                    else
                        rnt[9]++;
                }
                else {
                    rnt[7]++;
                }
            }
        }
        return rnt;
    }


    private Integer[] getNumbers206(List<FamilyMemberDTO> list){

        Integer[] rnt = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        for (int i = 0; i< list.size(); i++) {
            FamilyMemberDTO dto = list.get(i);

            if (dto.getState().equals("01") && dto.getIsSaveObject().equals("1"))
            {
                rnt[0]++;
                if (null != dto.getBirthday()) {
                    long bir = dto.getBirthday().getTime();
                    long now = System.currentTimeMillis();
                    long chang = 31536000000L;
                    long age = (now - bir) / chang;
                    if (age >= 60)
                        rnt[4]++;
                    else if (age >= 18)
                        rnt[5]++;
                    else
                        rnt[6]++;
                }
                else {
                    rnt[5]++;
                }
                if (!StringUtils.isEmpty(dto.getSex()) && dto.getSex().equals("2"))
                    rnt[1]++;
                if (!StringUtils.isEmpty(dto.getDisabilityType()))
                {
                    rnt[2]++;
                }
                if (!StringUtils.isEmpty(dto.getSelfcareLevel())) {
                    if (dto.getSelfcareLevel().equals("01"))
                        rnt[7]++;
                    else if (dto.getSelfcareLevel().equals("02"))
                        rnt[8]++;
                    else
                        rnt[9]++;
                }
                else {
                    rnt[7]++;
                }
            }
        }
        return rnt;
    }


    private Double[] getMonthMoney(String busiType,List<MemberSalaryEntity> list){
        Double[] rnt = {0.0,0.0,0.0};
        for(MemberSalaryEntity sa : list){
            Double salaryNumber = sa.getSalaryNumber();
            if(Constant.BusinessCode.DIBAO.equals(busiType)){
                if (sa.getSalaryType().equals("01") || sa.getSalaryType().equals("02"))
                    rnt[0] += salaryNumber;
                if (sa.getSalaryType().equals("03") || sa.getSalaryType().equals("04"))
                    rnt[2] += salaryNumber;
            }else if(Constant.BusinessCode.TEKUN.equals(busiType)){
                if(sa.getItemType().equals("310")){
                    rnt[0] += salaryNumber;
                }else{
                    rnt[1] += salaryNumber;
                }
            }
        }
        return rnt;
    }

    private void setFamMember206(SHJZT206Y19Entity ent,FamilyMemberDTO mh,int no){
        if (no == 0)
        {
            ent.setT206_JTCY_F40_Y19(mh.getName());
            ent.setT206_SFZH_F41_Y19(mh.getIdcard().toUpperCase());
        }
        if (no ==1)
        {
            ent.setT206_JTCY_F42_Y19(mh.getName());
            ent.setT206_SFZH_F43_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 2)
        {
            ent.setT206_JTCY_F44_Y19(mh.getName());
            ent.setT206_SFZH_F45_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 3)
        {
            ent.setT206_JTCY_F46_Y19(mh.getName());
            ent.setT206_SFZH_F47_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 4)
        {
            ent.setT206_JTCY_F48_Y19(mh.getName());
            ent.setT206_SFZH_F49_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 5)
        {
            ent.setT206_JTCY_F50_Y19(mh.getName());
            ent.setT206_SFZH_F51_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 6)
        {
            ent.setT206_JTCY_F52_Y19(mh.getName());
            ent.setT206_SFZH_F53_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 7)
        {
            ent.setT206_JTCY_F54_Y19(mh.getName());
            ent.setT206_SFZH_F55_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 8)
        {
            ent.setT206_JTCY_F56_Y19(mh.getName());
            ent.setT206_SFZH_F57_Y19(mh.getIdcard().toUpperCase());
        }
    }
    private void setFamMember207(SHJZT207Y19Entity ent,FamilyMemberDTO mh,int no){
        if (no == 0)
        {
            ent.setT207_JTCY_F40_Y19(mh.getName());
            ent.setT207_SFZH_F41_Y19(mh.getIdcard().toUpperCase());
        }
        if (no ==1)
        {
            ent.setT207_JTCY_F42_Y19(mh.getName());
            ent.setT207_SFZH_F43_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 2)
        {
            ent.setT207_JTCY_F44_Y19(mh.getName());
            ent.setT207_SFZH_F45_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 3)
        {
            ent.setT207_JTCY_F46_Y19(mh.getName());
            ent.setT207_SFZH_F47_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 4)
        {
            ent.setT207_JTCY_F48_Y19(mh.getName());
            ent.setT207_SFZH_F49_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 5)
        {
            ent.setT207_JTCY_F50_Y19(mh.getName());
            ent.setT207_SFZH_F51_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 6)
        {
            ent.setT207_JTCY_F52_Y19(mh.getName());
            ent.setT207_SFZH_F53_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 7)
        {
            ent.setT207_JTCY_F54_Y19(mh.getName());
            ent.setT207_SFZH_F55_Y19(mh.getIdcard().toUpperCase());
        }
        if (no == 8)
        {
            ent.setT207_JTCY_F56_Y19(mh.getName());
            ent.setT207_SFZH_F57_Y19(mh.getIdcard().toUpperCase());
        }
    }
}
